package cn.iocoder.yudao.module.shop.controller.admin.variable;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.shop.controller.admin.variable.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.variable.ShopVariableDO;
import cn.iocoder.yudao.module.shop.convert.variable.ShopVariableConvert;
import cn.iocoder.yudao.module.shop.service.variable.ShopVariableService;

@Tag(name = "管理后台 - 零散变量")
@RestController
@RequestMapping("/shop/variable")
@Validated
public class ShopVariableController {

    @Resource
    private ShopVariableService variableService;

    @PostMapping("/create")
    @Operation(summary = "创建零散变量")
    @PreAuthorize("@ss.hasPermission('shop:variable:create')")
    public CommonResult<Long> createVariable(@Valid @RequestBody ShopVariableCreateReqVO createReqVO) {
        return success(variableService.createVariable(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新零散变量")
    @PreAuthorize("@ss.hasPermission('shop:variable:update')")
    public CommonResult<Boolean> updateVariable(@Valid @RequestBody ShopVariableUpdateReqVO updateReqVO) {
        variableService.updateVariable(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除零散变量")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('shop:variable:delete')")
    public CommonResult<Boolean> deleteVariable(@RequestParam("id") Long id) {
        variableService.deleteVariable(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得零散变量")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('shop:variable:query')")
    public CommonResult<ShopVariableRespVO> getVariable(@RequestParam("id") Long id) {
        ShopVariableDO variable = variableService.getVariable(id);
        return success(ShopVariableConvert.INSTANCE.convert(variable));
    }

    @GetMapping("/list")
    @Operation(summary = "获得零散变量列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('shop:variable:query')")
    public CommonResult<List<ShopVariableRespVO>> getVariableList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopVariableDO> list = variableService.getVariableList(ids);
        return success(ShopVariableConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得零散变量分页")
    @PreAuthorize("@ss.hasPermission('shop:variable:query')")
    public CommonResult<PageResult<ShopVariableRespVO>> getVariablePage(@Valid ShopVariablePageReqVO pageVO) {
        PageResult<ShopVariableDO> pageResult = variableService.getVariablePage(pageVO);
        return success(ShopVariableConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出零散变量 Excel")
    @PreAuthorize("@ss.hasPermission('shop:variable:export')")
    @OperateLog(type = EXPORT)
    public void exportVariableExcel(@Valid ShopVariableExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopVariableDO> list = variableService.getVariableList(exportReqVO);
        // 导出 Excel
        List<ShopVariableExcelVO> datas = ShopVariableConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "零散变量.xls", "数据", ShopVariableExcelVO.class, datas);
    }

}
